package com.gao.web.controller;

import com.gao.web.service.UserService;
import net.sf.json.JSONObject;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/updateCustomer")
public class UpdateCustomerServlet extends HttpServlet {
    private UserService userService;

    @Override
    public void init() throws ServletException {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
        userService = context.getBean(UserService.class);
    }

    protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String jsonString = request.getParameter("d");
        JSONObject object = JSONObject.fromObject(jsonString);
        int id = Integer.parseInt(object.getString("id"));
        String username = object.getString("username");
        String password = object.getString("password");
        String name = object.getString("name");
        String ID_number = object.getString("ID_number");
        String birth_date = object.getString("birth_date");
        int sex = Integer.parseInt(object.getString("sex"));
        String email = object.getString("email");
        String address = object.getString("address");
        String phone = object.getString("phone");
        int i = userService.updateCustomer(id, username, password, name, ID_number, birth_date, sex, email, address, phone);
    }
}
